package com.tsingteng.cosfun.ui.message.fans;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.FansNotificationBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.TimeUtils;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class FansAdapter extends BaseQuickAdapter<FansNotificationBean.ListBean, BaseViewHolder> {

    private final FansPresenter fansPresenter;
    private OnClickFollowTypeListener onClickFollowTypeListener;

    public FansAdapter(FansPresenter fansPresenter) {
        super(R.layout.item_fans_adapter);
        this.fansPresenter = fansPresenter;

    }

    @Override
    protected void convert(BaseViewHolder helper, FansNotificationBean.ListBean item) {
        helper.setText(R.id.tv_fans_title, item.getSource().getNickName() + "");
        helper.getView(R.id.tv_fans_guan_zhu).setVisibility(View.VISIBLE);
        CircleImageView view = helper.getView(R.id.iv_fans_header);
        ImageUtils.LoadImage(mContext, item.getSource().getImage()+ Config.YA_SUO_IMAGE_URL, view);
        long createTime = item.getCreateTime();
        helper.setText(R.id.tv_fans_time, TimeUtils.formatDateTime(createTime));
        TextView textView = helper.getView(R.id.tv_fans_follow);
        int followType = item.getFollowType();
        if (followType == 0) {//未关注
            textView.setText("关注");
            textView.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_white));
            textView.setTextColor(mContext.getResources().getColor(R.color.black));
        } else if (followType == 1) {
            textView.setText("已关注");
            textView.setBackground(mContext.getResources().getDrawable(R.drawable.shape_trank_bg));
            textView.setTextColor(mContext.getResources().getColor(R.color.white));
        }else if (followType == 2){
            textView.setText("相互关注");
            textView.setBackground(mContext.getResources().getDrawable(R.drawable.shape_pink_bg));
            textView.setTextColor(mContext.getResources().getColor(R.color.color_found));
        }
        //判断是否已读
        ImageView iv_fans_isread = helper.getView(R.id.iv_fans_isread);
        int state = item.getState();
        if (state == 0) {
            iv_fans_isread.setVisibility(View.VISIBLE);
        } else {
            iv_fans_isread.setVisibility(View.GONE);
        }
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickFollowTypeListener!= null) {
                    onClickFollowTypeListener.onClickFolllowType(item,textView);
                }
//                if (followType == 0) {
//                    textView.setText("已关注");
//                    fansPresenter.loginFollowFans(item.getSource().getId() + "");
//                } else  if (followType == 1){
//                    textView.setText("关注");
//                    textView.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_white));
//                    textView.setTextColor(mContext.getResources().getColor(R.color.black));
//                    fansPresenter.loginNoFollow(item.getSource().getId() + "");
//                } if (followType == 2){
//                    textView.setText("未关注");
//                    textView.setBackground(mContext.getResources().getDrawable(R.drawable.shape_trank_bg));
//                    textView.setTextColor(mContext.getResources().getColor(R.color.white));
//                }
            }
        });

        helper.getView(R.id.iv_fans_header).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tag = 1;
                if (item.getSource().getId() != SPUtils.init().getInt("profile_id")) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("tag", tag);
                    bundle.putInt("user_id", item.getSource().getId());
                    bundle.putInt("workId", item.getSource().getId());
                    TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("tag", tag);
                    bundle.putInt("user_id", item.getTarget().getId());
                    bundle.putInt("workId", item.getTarget().getId());
                    TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
                }
            }
        });
    }

    public interface OnClickFollowTypeListener{
        void onClickFolllowType(FansNotificationBean.ListBean followType, TextView textView);
    }


    public void setOnClickFollowTypeListener(OnClickFollowTypeListener onClickFollowTypeListener) {
        this.onClickFollowTypeListener = onClickFollowTypeListener;
    }
}
